package com.bigcat.gulimall.gulimallproduct.controller;

import com.bigcat.gulimall.common.utils.PageUtils;
import com.bigcat.gulimall.common.utils.R;
import com.bigcat.gulimall.gulimallproduct.entity.CategoryEntity;
import com.bigcat.gulimall.gulimallproduct.service.CategoryService;
import com.bigcat.gulimall.gulimallproduct.valid.AddGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 商品三级分类
 *
 * @author sufer
 * @email 123@qq.com
 * @date 2020-09-07 16:03:44
 */
@RestController
@RequestMapping("gulimallproduct/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @Cacheable(value = "category",key = "#root.methodName",sync = true)
    //@RequiresPermissions("gulimallproduct:category:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = categoryService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    //@RequiresPermissions("gulimallproduct:category:info")
    public R info(@PathVariable("catId") Long catId) {
        CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("gulimallproduct:category:save")
    public R save(@Validated(value = {AddGroup.class}) @RequestBody CategoryEntity category) {
        boolean result = categoryService.save(category);

        return R.ok().put("result", result);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("gulimallproduct:category:update")
    public R update(@RequestBody CategoryEntity category) {
        categoryService.updateById(category);

        return R.ok();
    }

    /**
     * CacheEvict：移除缓存，适用于失效模式
     * CachePut：缓存更新：使用域双写模式
     * Caching：组合缓存操作
     * <p>
     * key:其值格式为SPEL表达式，如果是普通字符串一定要加上单引号
     *
     * @param category
     * @return
     */
    @RequestMapping("/update/sort")
    //删除category分区下所有缓存
//    @CacheEvict(value = "category", allEntries = true)
    @Caching(evict = {
            @CacheEvict(value = "category",key = "'listWithTree'"),
            @CacheEvict(value = "category",key = "'list'")
    })
    public R updateSort(@RequestBody CategoryEntity[] category) {
        categoryService.updateBatchById(Arrays.asList(category));
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("gulimallproduct:category:delete")
    public R delete(@RequestBody Long[] catIds) {
        //删除全需要判断菜单是否被其他引用
        categoryService.removeMenuByIds(Arrays.asList(catIds));

        return R.ok();
    }

    /**
     * 列表
     *
     * @return
     */
    @RequestMapping("/list/tree")
    public R list() {
        List<CategoryEntity> categoryEntities = categoryService.listWithTree();

        return R.ok().put("data", categoryEntities);
    }


}
